/*
 * Created on 2005-8-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.store;

import com.jedi.BaseObj;
import com.jedi.KeyGen;
import com.tss.util.DbConn;
import com.tss.util.DbRs;

/**
 * @author dai
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Download extends BaseObj {
	public Download() {}
	public Download(String id) {this.softId = id;}
	
	public void increase() {
		clearErr();
		DbConn conn = new DbConn();
		try {
			if (getId().trim().equals("")) {
				setErr("û��Ҫ���µļ�¼ID��");
				return;
			}
			String sql = "update t_download_info set" +
				" hit = hit + 1 where soft_id = ?";
			conn.prepare(sql);
			conn.setString(1,getId());
			conn.executeUpdate();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
		}
		finally {
			conn.close();
		}
	}
	
	public void insert() {
		clearErr();
		DbConn conn = new DbConn();
		try {
			String sql = "";
			setId(KeyGen.nextID(""));
			sql = "select * from t_soft_category where category_id = ?";
			conn.prepare(sql);
			conn.setString(1,getCategoryId());
			DbRs rs = conn.executeQuery();
			if (rs == null || rs.size() == 0) {
				setErr("ѡ��ķ��಻����!");
				return;
			}
			sql = "insert into t_download_info (" +
				"soft_id,soft_name,author_id,author_name,soft_desc,category_id,soft_size," +
				"soft_type,os_type,soft_language,commend_level,issue_time," +
				"download_url,hit" +
				") values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			conn.prepare(sql);
			conn.setString(1,getId());
			conn.setString(2,getSoftName());
			conn.setString(3,getAuthorId());
			conn.setString(4,getAuthorName());
			conn.setString(5,getSoftDesc());
			conn.setString(6,getCategoryId());
			conn.setInt(7,getSoftSize());
			conn.setString(8,getSoftType());
			conn.setString(9,getOsType());
			conn.setString(10,getSoftLanguage());
			conn.setInt(11,getCommendLevel());
			conn.setString(12,getIssueTime());
			conn.setString(13,getDownloadUrl());
			conn.setInt(14,0);
			conn.executeUpdate();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
		}
		finally {
			conn.close();
		}
	}
	
	public void update() {
		clearErr();
		DbConn conn = new DbConn();
		try {
			if (getId().trim().equals("")) {
				setErr("û��Ҫ���µļ�¼ID��");
				return;
			}
			String sql = "update t_download_info set" +
				" soft_name = ?,author_id = ?,author_name = ?,soft_desc = ?,category_id = ?," +
				" soft_size = ?,soft_type = ?,os_type = ?,soft_language = ?,commend_level = ?," +
				" download_url = ?,issue_time = ?" +
				" where soft_id = ?";
			conn.prepare(sql);
			conn.setString(1,getSoftName());
			conn.setString(2,getAuthorId());
			conn.setString(3,getAuthorName());
			conn.setString(4,getSoftDesc());
			conn.setString(5,getCategoryId());
			conn.setInt(6,getSoftSize());
			conn.setString(7,getSoftType());
			conn.setString(8,getOsType());
			conn.setString(9,getSoftLanguage());
			conn.setInt(10,getCommendLevel());
			conn.setString(11,getDownloadUrl());
			conn.setString(12,getIssueTime());
			conn.setString(13,getId());
			conn.executeUpdate();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
		}
		finally {
			conn.close();
		}
	}
	
	public void delete() {
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "";
			if (getId().trim().equals("")) {
				setErr("û��Ҫɾ��ļ�¼!");
				return;
			}
			sql = "delete from t_download_info where soft_id = ?";
			conn.prepare(sql);
			conn.setString(1,getId());
			conn.executeUpdate();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
		}
		finally {
			conn.close();
		}
	}
	
	public String getId() {return softId;}
	public String getSoftName() {return softName;}
	public String getAuthorId() {return authorId;}
	public String getAuthorName() {return authorName;}
	public String getSoftDesc() {return softDesc;}
	public String getCategoryId() {return categoryId;}
	public DownCate getCate() {return downCate;}
	public int getSoftSize() {return softSize;}
	public String getSoftType() {return softType;}
	public String getOsType() {return osType;}
	public String [] getOsTypeList() {
		if (osType == null || osType.trim().equals(""))
			return null;
		return osType.split(",");
	}
	public String getSoftLanguage() {return softLanguage;}
	public int getCommendLevel() {return commendLevel;}
	public String getDownloadUrl() {return downloadUrl;}
	public String getIssueTime() {return issueTime ;}
	public int getHit() {return hit;}

	public void setId(String softId) {this.softId = softId;}
	public void setSoftName(String softName) {this.softName = softName;}
	public void setAuthorId(String authorId) {this.authorId = authorId ;}
	public void setAuthorName(String authorName) {this.authorName = authorName;}
	public void setSoftDesc(String softDesc) {this.softDesc = softDesc;}
	public void setCategoryId(String categoryId) {this.categoryId = categoryId;}
	public void setCate(DownCate cate) {this.downCate = cate;}
	public void setSoftSize(int softSize) {this.softSize = softSize;}
	public void setSoftType(String softType) {this.softType = softType;}
	public void setOsType(String osType) {this.osType = osType;}
	public void setOsType(String [] osType) {
		String r = "";
		if (osType == null || osType.length == 0) {
			this.osType = "";
			return;
		}
		for(int i=0;i<osType.length;i++) {
			if (!r.trim().equals("")) r += ",";
			r += osType[i];
		}
		this.osType = r;
		return;
	}
	public void setSoftLanguage(String softLanguage) {this.softLanguage = softLanguage;}
	public void setCommendLevel(int commendLevel) {this.commendLevel = commendLevel;}
	public void setDownloadUrl(String downloadUrl) {this.downloadUrl = downloadUrl;}
	public void setIssueTime(String issueTime) {this.issueTime = issueTime ;}
	public void setHit(int hit) {this.hit = hit;}

	private String softId = "";
	private String softName = "";
	private String authorId     = "";
	private String authorName   = "";
	private String softDesc = "";
	private String categoryId = "";
	private DownCate downCate = null;
	private int softSize = 0;
	private String softType = "";
	private String osType = "";
	private String softLanguage = "";
	private int commendLevel = 0;
	private String downloadUrl = "";
	private String issueTime = "";
	private int hit = 0;
}
